
cc.Class({
    extends: cc.Component,

    properties: {
        launcher : require('SkillLauncher'),
        players : [cc.Node],
        farms:[cc.SpriteFrame],
        mask:cc.Node,
        leftSelectBtn : cc.Node,
        rightSelectBtn : cc.Node,
        childIcon:cc.Sprite,
        rewardTips:cc.Label,
        content:cc.Node,
        scroll:require('UI-ScrollSelect'),
        left:cc.Node,
        right:cc.Node
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.n_fireTime = 0;
        // this.oldPlaneId = 0;
        this.n_playerId = 0;
        
       global.Event.on('showSup',this.changeSup.bind(this),this)

    }, 
    onDestroy() {
        global.Event.off('showSup')
    },

    start () {
        this.sup0=cc.find('supLauncher/left/launcher',this.node).getComponent('SkillLauncher')
        this.sup1=cc.find('supLauncher/right/launcher',this.node).getComponent('SkillLauncher')
        this.launcher.setSkillParentNode(this.launcher.node,true);
        this.sup0.setSkillParentNode(this.launcher.node,true);
        this.sup1.setSkillParentNode(this.launcher.node,true);
    },

    changeSup(data){
        this.setSupNumber(data.data)
    },


    setPlane(data){
        this.unscheduleAllCallbacks()
        this.ani(this.n_playerId).play(this.ani(this.n_playerId)._clips[1].name);
        this.removeBaozou();
        if(data.type==0){
            this.sup0.reset();
            this.sup1.reset();
            this.launcher.reset();
        }else if(data.type==2){
            global.planeId = data.index;
            this.n_playerId = data.index;
            this.level = 4;
            this.n_fireTime = 5;
            this.startFire();
            var siginCount=global.Data.signIn.signCount;
            var gatesCount=global.Data.currentPass;
            if(data.index==2){
                this.rewardTips.node.parent.active = siginCount<3
                this.rewardTips.string='第三日登录可领取'
            }else if(data.index==1){
                this.rewardTips.node.parent.active = gatesCount<5
                this.rewardTips.string='通过第五关可领取'
            }else{
                this.rewardTips.node.parent.active=false
            }
            global.Event.emit('STARTBTN',this.rewardTips.node.parent.active);
            this.changePlane(data.index)
            this.supFire();
        }
    },

    setSupId(supId){
        if(supId>=0){
            this.supNumber=supId;//0,1两种副武器
            this.setSup();
            this.supFire();
        }
    },
    setSup(){
        cc.find('supLauncher/left',this.node).getComponent(cc.Sprite).spriteFrame=this.farms[this.supNumber];
        cc.find('supLauncher/right',this.node).getComponent(cc.Sprite).spriteFrame=this.farms[this.supNumber]; 
        this.childIcon.spriteFrame=this.farms[this.supNumber]; 
    },

    setSupNumber(supId){
        this.supNumber=supId;
        this.setSup();
        this.supFire();
    },

    changePlane(id){
        for(let i=0;i<3;i++){
            this.left.children[i].active=false
            this.right.children[i].active=false
        }
        if(id==0){
            this.left.children[2].active=true
            this.right.children[1].active=true
        }else if(id==1){
            this.left.children[0].active=true
            this.right.children[2].active=true
        }else if(id==2){
            this.left.children[1].active=true
            this.right.children[0].active=true
        }
    },

    clickplane(e,d){
        this.scroll.scrollTo(this.scroll.currentIndex-parseInt(d))
    },

    removeBaozou(){
        this.players[this.n_playerId].getChildByName('baozou').active=false;
    },

    startFire(){
        this.launcher.reset();
        var option = global.DataConfig.monster['player'+(this.n_playerId+1)]
        var skills = option.status[Math.min(4,this.level)];

        this.launcher.addLauncher(option.status[Math.min(4,3)])
        if(this.level===4){
            this.ani(this.n_playerId).play(this.ani(this.n_playerId)._clips[0].name);
            this.scheduleOnce(()=>{
                this.players[this.n_playerId].getChildByName('baozou').active=true;
                this.launcher.reset();
                for(let i=0;i<skills.length;i++){
                    if(skills[i].name){
                        skills[i].speed=global.SkillConfig[skills[i].name].speed*(global.Data.speedLevel*0.02+1)
                        skills[i].emissionRate=Math.floor(global.SkillConfig[skills[i].name].emissionRate*(global.Data.speedLevel*0.02+1))
                    }
                    
                }
                this.launcher.addLauncher(skills)
            },0.5)
        }else{
            this.ani(this.n_playerId).play(this.ani(this.n_playerId)._clips[1].name);
            this.players[this.n_playerId].getChildByName('baozou').active=false;
            this.launcher.reset();
            for(let i=0;i<skills.length;i++){
                if(skills[i].name){
                    skills[i].speed=global.SkillConfig[skills[i].name].speed*(global.Data.speedLevel*0.02+1)
                    skills[i].emissionRate=Math.floor(global.SkillConfig[skills[i].name].emissionRate*(global.Data.speedLevel*0.02+1))
                }
                
            }
            this.launcher.addLauncher(skills)
        }
    },

    supFire(){
        this.sup0.reset();
        this.sup1.reset();
        var optionSup=global.DataConfig.monster['supLaunch'+(this.supNumber+1)]
        var supSkills=optionSup.status['0']
        this.sup0.grow = 1 + global.Data.childArmsLevel/10 
        this.sup1.grow = 1 + global.Data.childArmsLevel/10 
        this.sup0.addLauncher(supSkills)
        this.sup1.addLauncher(supSkills)
    },


    ani(id){
        return this.players[id].getChildByName('change1').getComponent(cc.Animation)
    },

    Strengthen(){
        SoundMgr.playSound('Audio/button')
        global.EJMgr.pushUI('PlaneStrengthen/PlaneStrengthen')
    },
    update (dt) {
        // if(this.n_fireTime > 0){
            this.n_fireTime -= dt;
            if(this.n_fireTime < 0){
                this.n_fireTime = 5
                this.level = (this.level == 3 ? 4 : 3);
                this.startFire()
            }
        // }
    },
});
